package com.sctu.edu.tracks;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sctu.edu.tracks.utils.JumpUtils;
import com.sctu.edu.tracks.utils.L;
import com.sctu.edu.tracks.utils.httputils.NetworkUtils;
import com.sctu.edu.tracks.utils.httputils.SharePreference;


public class StartLogoActivity extends Activity {

    private EditText usernameEditText;
    private String username;
    private Button confirm;
    private NetworkUtils networkUtils;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_startlogo);
        usernameEditText = (EditText) findViewById(R.id.username_editText);
        confirm = (Button) findViewById(R.id.username_confirm_btn);
        networkUtils = NetworkUtils.getIntance(this);

        if (networkUtils.isNetworkConnected()) {

            confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setEditTextInhibitInputSpace(usernameEditText);
                    if (!TextUtils.isEmpty(usernameEditText.getText())) {
                        username = usernameEditText.getText().toString();

                        //执行页面跳转工作
                        final Handler handler = new Handler() {
                            @Override
                            public void handleMessage(Message msg) {
                                super.handleMessage(msg);
                                Bundle bundle = new Bundle();
                                bundle.putString("username", username);
                                JumpUtils.activityToFragmentJump(StartLogoActivity.this, bundle);
                            }

                        };

                        Timer timer = new Timer();
                        TimerTask timertask = new TimerTask() {

                            @Override
                            public void run() {
                                Message msg = new Message();
                                handler.sendMessage(msg);
                            }
                        };

                        timer.schedule(timertask, 1000);
                    } else {
                        L.e("confirm", "is null");
                    }
                }
            });
        } else {
            Toast.makeText(this, "请先联网", Toast.LENGTH_LONG).show();
        }
    }


    /**
     * 禁止EditText输入空格
     *
     * @param editText
     */
    public static void setEditTextInhibitInputSpace(EditText editText) {
        InputFilter filter = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" ")) {
                    return "";
                } else {
                    return null;
                }
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }

}
